var util = require('../../libs/util.js');
var moment = require('../../libs/moment.min.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    pageSize: 10,
    pageNumber: 0,
    count: 0,
    page: 1,
    firstPage: true,
    prevPage: true,
    nextPage: true,
    lastPage: true,
    imgList:[],
    formType : 'add'
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": "车辆档案" });
    var that = this;
    //更新数据
    that.setData({
      vehicle_id: e.id,
      tab: e.tab || 3,
      role_default:app.globalData.role_default,
      server:app.server
    });
    this.loadData();
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  search: function () {
    this.loadData();
  },
  loadData: function () {
    var that = this;
    wx.showLoading({
      title: '请稍候...',
    });
    util.getData("/atapi/vehicle/vehicle/before_check", {
      'CO_ID': app.globalData.CO_ID,
      vehicle_id: that.data.vehicle_id,
      'searchVal': that.data.searchVal,
      'page': that.data.page,
      'pageSize': that.data.pageSize,
      'outsideUser': that.data.outsideUser
    }, function (res, error) {
      console.log(res); console.log(error)
      setTimeout(function(){
        wx.hideLoading();
      },100);
      if (res.data.success) {
        wx.setNavigationBarTitle({ "title": "车辆出车记录--" + res.data.data[0][0].plate });
        var count = res.data.data[2][0].COUNT;
        var pageNumber = Math.ceil(count / that.data.pageSize);
        that.setData({
          plate:res.data.data[0][0].plate,
          d_user_id:res.data.data[0][0].d_user_id,
          user_id:app.globalData.user_id,
          driverName:res.data.data[0][0].name,
          is_fleet:app.globalData.role_default == 'fleet'?true:false,
          dataList: res.data.data[1],
          count: count,
          pageNumber: pageNumber,
          page: that.data.page == 0 ? 1 : that.data.page
        });
        that.setPageStatus();
      } else {
        that.setData({
          page: 0,
          dataList: []
        });
      }
    });
  },
  //翻页按钮状态
  setPageStatus: function () {
    var that = this;
    var page = that.data.page, pageNumber = that.data.pageNumber;
    if (pageNumber == 1) {
      that.setData({
        firstPage: true,
        prevPage: true,
        nextPage: true,
        lastPage: true
      });
    } else if (pageNumber > 1) {
      if (page == 1) {
        that.setData({
          firstPage: true,
          prevPage: true,
          nextPage: false,
          lastPage: false
        });
      } else if (page == pageNumber) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: true,
          lastPage: true
        });
      } else if (page < pageNumber && page > 1) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: false,
          lastPage: false
        });
      }
    }
  },
  //分页按钮操作
  pageChange: function (e) {
    var type = e.currentTarget.dataset.page;
    if (type == 'first') {
      this.setData({
        page: 1
      })
      this.loadData();
    }
    if (type == 'last') {
      this.setData({
        page: this.data.pageNumber
      })
      this.loadData();
    }
    if (type == 'prev') {
      this.setData({
        page: this.data.page - 1
      })
      this.loadData();
    }
    if (type == 'next') {
      this.setData({
        page: this.data.page + 1
      })
      this.loadData();
    }
  },
  showModal(e) {
    var that = this;
    this.setData({
      modalName: e.currentTarget.dataset.target,
      modalTitle: e.currentTarget.dataset.title,
      modalType:e.currentTarget.dataset.type
    })
    if(e.currentTarget.dataset.type == 'detail'){
       var id = e.currentTarget.id;
       var dataList = that.data.dataList;
       for(var i=0;i<dataList.length;i++){
         if(dataList[i].ID == id){
           var currentData = [];
           currentData.push({name:'总公里数',value:dataList[i].mile});
           currentData.push({name:'油表(格)/电量(%)',value:dataList[i].oil_gauge});
           currentData.push({name:'证件齐全',value:dataList[i].credentials == 1?'是':'否'});
           currentData.push({name:'车灯',value:dataList[i].light == 1?'正常':'异常'});
           currentData.push({name:'胎压',value:dataList[i].tire_pressure == 1?'正常':'异常'});
           currentData.push({name:'胎纹',value:dataList[i].tire_tread == 1?'正常':'异常'});
           currentData.push({name:'冷机',value:dataList[i].cold_machine == 1?'正常':'异常'});
           currentData.push({name:'车身',value:dataList[i].bodywork == 1?'正常':'异常'});
           currentData.push({name:'机油',value:dataList[i].engine_oil == 1?'正常':'异常'});
           currentData.push({name:'门窗',value:dataList[i].door_win == 1?'正常':'异常'});
           currentData.push({name:'挡风玻璃',value:dataList[i].windshield == 1?'正常':'异常'});
           currentData.push({name:'水箱',value:dataList[i].water_tank == 1?'正常':'异常'});
           currentData.push({name:'雨刮水',value:dataList[i].glass_cleaner == 1?'正常':'异常'});
           currentData.push({name:'刹车',value:dataList[i].brake == 1?'正常':'异常'});
            that.setData({
              _currentData:dataList[i],
               currentData:currentData
            })
         }
       }
    }
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  showPictrue: function (e) {
    var image = e.currentTarget.dataset.image;
    wx.previewImage({
      urls: image
    })
  },
  previewImage: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.name;
    var current = e.currentTarget.dataset.url
    var imgList = that.data.imgList;
    wx.previewImage({
      current: current,
      urls: imgList
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})